package com.dci.service.impl.project;

import com.alibaba.fastjson.JSONObject;
import com.dci.context.Config;
import com.dci.dao.pms.project.PmsItemInputMapper;
import com.dci.dao.pms.schedule.PmsCapitalContractMapper;
import com.dci.dao.pms.schedule.ProgressTrackMapper;
import com.dci.model.project.PmsItemInput;
import com.dci.model.schedule.PmsCapitalContract;
import com.dci.model.schedule.ProgressTrack;
import com.dci.result.ResultInfo;
import com.dci.result.ResultUtil;
import com.dci.service.project.PmsItemInputService;
import com.dci.service.schedule.PmsCapitalContractService;
import com.dci.util.UUIDBuild;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.service.impl.project
 * @Description: 建设单位业务录入实现类
 * @author: njj
 * @date: 2019/2/25
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PmsItemInputServiceImpl implements PmsItemInputService {

    @Resource
    private PmsItemInputMapper pmsItemInputMapper;

    @Resource
    private ProgressTrackMapper progressTrackMapper;

    @Resource
    private PmsCapitalContractMapper pmsCapitalContractMapper;

    @Resource
    private PmsCapitalContractService pmsCapitalContractService;

    @Resource
    private PmsItemInputService pmsItemInputService;

    /**
     * 添加与保存录入数据
     *
     * @param object
     * @param path
     * @param fileNames
     * @return
     */
    @Override
    public ResultInfo uploadAndSave(String object, String path, String fileNames, String contractUrlPath, String contractFiles, String contractStatus) {
        JSONObject jsonObject = JSONObject.parseObject(object);
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(now);
        String id = UUIDBuild.getUUID();
        String contractId = "";
        PmsItemInput pmsItemInput = JSONObject.toJavaObject(jsonObject, PmsItemInput.class);
        pmsItemInput.setContractStatus(contractStatus);
        List< PmsItemInput > pmsItemInputList = pmsItemInputService.selectAll();
        if ("合同".equals(pmsItemInput.getMoneyType())) {
            if (pmsItemInput.getId() == null) {
                for (int i = 0; i < pmsItemInputList.size(); i++) {
                    if (pmsItemInput.getFileName() == pmsItemInputList.get(i).getFileName() || pmsItemInput.getFileName().equals(pmsItemInputList.get(i).getFileName())) {
                        return ResultUtil.createWarning(Config.MESSAGE, 911, null);
                    } else {
                        contractId = UUIDBuild.getUUID();
                        PmsCapitalContract pmsCapitalContract = PmsCapitalContract.builder().id(contractId).capitalId(pmsItemInput.getProjectId()).fileName(pmsItemInput.getFileName()).
                                contractMoney(String.valueOf(pmsItemInput.getMoneyNum())).contractNumber(pmsItemInput.getFileNum()).contractFile(contractFiles).contractUrl(contractUrlPath).contractStatus(0).build();
                        pmsCapitalContractService.addContract(pmsCapitalContract);
                        pmsItemInput.setContractId(contractId);
                    }
                }
            } else {
                for (int i = 0; i < pmsItemInputList.size(); i++) {
                    if (pmsItemInput.getFileName() == pmsItemInputList.get(i).getFileName() || pmsItemInput.getFileName().equals(pmsItemInputList.get(i).getFileName())) {
                        return ResultUtil.Warning(0, null);
                    } else {
                        contractId = pmsItemInput.getContractId();
                        PmsCapitalContract pmsCapitalContract = PmsCapitalContract.builder().id(contractId).capitalId(pmsItemInput.getProjectId()).fileName(pmsItemInput.getFileName()).
                                contractMoney(String.valueOf(pmsItemInput.getMoneyNum())).contractNumber(pmsItemInput.getFileNum()).contractFile(fileNames).contractUrl(contractUrlPath).opinion(pmsItemInput.getOpinion()).contractStatus(0).build();
                        pmsCapitalContractService.updateContract(pmsCapitalContract);
                        pmsItemInput.setContractId(contractId);
                    }
                }
            }
        }

        if ("立项".equals(pmsItemInput.getMoneyType()) && pmsItemInput.getId() == null) {
            List< PmsItemInput > list = pmsItemInputMapper.selectByTimeAndId(pmsItemInput);
            if (list.size() > 0) {
                pmsItemInputMapper.deleteByPrimaryKey(list.get(0).getId());
                pmsItemInput.setId(list.get(0).getId());
                pmsItemInput.setFileAddress(path);
                pmsItemInput.setFileNames(fileNames);
                pmsItemInput.setContractPath(contractUrlPath);
                pmsItemInput.setContractFile(contractFiles);
                pmsItemInput.setTime(time);
                pmsItemInput.setAuditStatus(0);
                pmsItemInputMapper.insert(pmsItemInput);
            } else {
                pmsItemInput.setId(id);
                pmsItemInput.setFileAddress(path);
                pmsItemInput.setFileNames(fileNames);
                pmsItemInput.setContractPath(contractUrlPath);
                pmsItemInput.setContractFile(contractFiles);
                pmsItemInput.setTime(time);
                pmsItemInput.setAuditStatus(0);
                pmsItemInputMapper.insert(pmsItemInput);
            }
        } else {
            if (pmsItemInput.getId() == null) {
                pmsItemInput.setId(id);
                pmsItemInput.setFileAddress(path);
                pmsItemInput.setFileNames(fileNames);
                pmsItemInput.setContractPath(contractUrlPath);
                pmsItemInput.setContractFile(contractFiles);
                pmsItemInput.setTime(time);
                pmsItemInput.setAuditStatus(0);
                int ret = pmsItemInputMapper.insert(pmsItemInput);
            } else {
                pmsItemInputMapper.deleteByPrimaryKey(pmsItemInput.getId());
                pmsItemInput.setFileAddress(path);
                pmsItemInput.setFileNames(fileNames);
                pmsItemInput.setContractPath(contractUrlPath);
                pmsItemInput.setContractFile(contractFiles);
                pmsItemInput.setTime(time);
                pmsItemInput.setAuditStatus(0);
                pmsItemInputMapper.insert(pmsItemInput);
            }
        }
        return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List< PmsItemInput > selectAll() {
        List< PmsItemInput > list = pmsItemInputMapper.selectAll();
        if (list.size() > 0) {
            for (PmsItemInput pmsItemInput : list) {
                List< PmsCapitalContract > list1 = pmsCapitalContractMapper.selectById(pmsItemInput.getContractId());
                if (list1.size() > 0) {
                    for (PmsCapitalContract pmsCapitalContract : list1) {
                        List< PmsItemInput > list2 = pmsItemInputMapper.selectByContractId(pmsCapitalContract.getId());
                        pmsCapitalContract.setList(list2);
                    }
                }
                pmsItemInput.setList(list1);
            }
        }
        return list;
    }

    /**
     * 修改状态
     *
     * @param pmsItemInput
     * @return
     */
    @Override
    public ResultInfo updateStatus(PmsItemInput pmsItemInput) {
        ProgressTrack model = progressTrackMapper.selectById(pmsItemInput.getProjectId());
        String moneyNum = model.getItem_amount();
        String type = model.getItemType();
        if (pmsItemInput.getAuditStatus() == 1) {
            if ("立项(项目建议书批复)".equals(model.getItemType())) {
                moneyNum = String.valueOf(pmsItemInput.getMoneyNum());
                type = pmsItemInput.getMoneyType();
            } else if ("立项(可行性研究批复)".equals(model.getItemType())) {
                if (pmsItemInput.getMoneyType().equals("立项(可行性研究批复)") || pmsItemInput.getMoneyType().equals("立项(初步设计批复)")) {
                    moneyNum = String.valueOf(pmsItemInput.getMoneyNum());
                    type = pmsItemInput.getMoneyType();
                }
            } else if ("立项(初步设计批复)".equals(model.getItemType())) {
                if (pmsItemInput.getMoneyType().equals("立项(初步设计批复)")) {
                    moneyNum = String.valueOf(pmsItemInput.getMoneyNum());
                    type = pmsItemInput.getMoneyType();
                }
            } else {
                moneyNum = String.valueOf(pmsItemInput.getMoneyNum());
                type = pmsItemInput.getMoneyType();
            }
            ProgressTrack progressTrack = ProgressTrack.builder().id(pmsItemInput.getProjectId()).item_amount(moneyNum).build();
            progressTrack.setItemType(type);
            progressTrackMapper.update(progressTrack);
            pmsItemInput.setContractStatus("1");
        }

        PmsCapitalContract pmsCapitalContract = PmsCapitalContract.builder().id(pmsItemInput.getContractId()).opinion(pmsItemInput.getOpinion()).contractStatus(pmsItemInput.getAuditStatus()).build();
        pmsCapitalContractMapper.updateContractStatus(pmsCapitalContract);
        int i = pmsItemInputMapper.updateByPrimaryKeySelective(pmsItemInput);
        if (i > 0) {
            return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
        }
        return ResultUtil.createSuccess(Config.MESSAGE, 911, null);
    }

    /**
     * 通过时间id查询
     *
     * @param pmsItemInput
     * @return
     */
    @Override
    public List< PmsItemInput > selectByTimeAndId(PmsItemInput pmsItemInput) {
        List< PmsItemInput > list = pmsItemInputMapper.selectByTimeAndId(pmsItemInput);
        return list;
    }

    /**
     * 删除
     *
     * @param pmsItemInput
     * @return
     */
    @Override
    public ResultInfo deleteById(PmsItemInput pmsItemInput) {
        int i = pmsItemInputMapper.deleteByPrimaryKey(pmsItemInput.getId());
        if (i > 0) {
            return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
        }
        return ResultUtil.createSuccess(Config.MESSAGE, 911, null);
    }


    /**
     * 删除
     *
     * @param pmsItemInput
     * @return
     */
    @Override
    public ResultInfo deleteByProjectId(PmsItemInput pmsItemInput) {
        int i = pmsItemInputMapper.deleteByProjectId(pmsItemInput.getProjectId());
        if (i > 0) {
            return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
        }
        return ResultUtil.createSuccess(Config.MESSAGE, 911, null);
    }

    /**
     * 通过projectid 查询信息
     *
     * @param pmsItemInput
     * @return
     */
    @Override
    public List< PmsItemInput > selectByProjectId(PmsItemInput pmsItemInput) {
        return pmsItemInputMapper.selectByProjectId(pmsItemInput);
    }

    @Override
    public List< PmsItemInput > selectByProjectName(String itemName, String projectName) {
        return pmsItemInputMapper.selectByProjectName(itemName, projectName);
    }
}
